
#include <helper.h>
#include <qwidget.h>
#include <qsplitter.h>
#include <qdir.h>
#include <qdiriterator.h>
#include <qfile.h>

#define FRAME_TIME	50


bool cceDirRemoverHelper::RecursiveRemoveDir(QDir aDir)
{
	bool allOk = true;
	if (aDir.exists())//QDir::NoDotAndDotDot
		{
			QFileInfoList entries = aDir.entryInfoList(	QDir::NoDotAndDotDot |
																									QDir::Dirs |
																									QDir::Files);
			foreach (QFileInfo entryInfo, entries)
				{
					QString path = entryInfo.absoluteFilePath();
					if (entryInfo.isDir())
						{
							allOk = RecursiveRemoveDir(QDir(path));
						}
					else
						{
							QFile file(path);
							if (!file.remove())
								{
									allOk = false;
								}
						}
					if (!allOk)
						{
							break;
						}
				}
			if (!aDir.rmdir(aDir.absolutePath()))
				{
					allOk = false;
				}
		}
	return allOk;
}

cceResizeHelper::cceResizeHelper (QWidget *target, int width, int height)
  : QObject ()
  , _widget(target)
  , _width (width)
  , _height (height)
{

  QTimer::singleShot(100, this, SLOT(timeout()));
}

void cceResizeHelper::timeout()
{
  _widget->resize(_width, _height);
  deleteLater();
}

cceSliderHelper::cceSliderHelper (QWidget *widget, int time, int step)
  : QObject ()
  , _widget(widget)
  , _ms (time)
{
  _time.start();
  _timer.setInterval(step);
  _timer.setSingleShot(false);
  connect (&_timer, SIGNAL(timeout()), this, SLOT(timeout()));
  connect (_widget, SIGNAL(destroyed()), this, SLOT(clear()));
  _timer.start();
}

void cceSliderHelper::clear()
{
  _timer.stop();
  _widget = 0;
  deleteLater();

}



void cceSliderHelper::timeout()
{
  if (!_widget) return;


  float a = (float)_time.elapsed() / (float)_ms;
  bool clr = false;
  if (a >= 1.0f)
    {
      clr = true;
      a = 1.0f;
    }

  update (_widget, a);
  if (clr)
    {
      clear();
    }
}


cceResizeSliderHelper::cceResizeSliderHelper (QWidget *target, int width, int height, int time, int step)
  : cceSliderHelper (target, time, step)
  , _widthTo (width)
  , _heightTo (height)
{
  _widthFrom = target->width();
  _heightFrom = target->height();
}

void cceResizeSliderHelper::update(QWidget *widget, float a)
{
  float width = (float)_widthFrom + (float)(_widthTo - _widthFrom) * a;
  float height= (float)_heightFrom + (float)(_heightTo - _heightFrom) * a;

  widget->resize(width, height);
}






ccePosSliderHelper::ccePosSliderHelper (QWidget *target, int x, int y, int time, int step)
  : cceSliderHelper (target, time, step)
  , _xTo (x)
  , _yTo (y)
{
  _xFrom = target->pos().x();
  _yFrom = target->pos().y();

}

void ccePosSliderHelper::update(QWidget *widget, float a)
{
  float x = (float)_xFrom + (float)(_xTo - _xFrom) * a;
  float y = (float)_yFrom + (float)(_yTo - _yFrom) * a;

  widget->move (x, y);
}





cceSplitterSliderHelper::cceSplitterSliderHelper (QSplitter *target, QList<int> sizes, int time, int step)
  : cceSliderHelper (target, time, step)
  , _sizesTo (sizes)
{
  _sizesFrom = target->sizes();
}

void cceSplitterSliderHelper::update(QWidget *widget, float a)
{
  QSplitter* splitter = static_cast<QSplitter*>(widget);

  QList<int> sizes;
  for (unsigned i=0, j=_sizesTo.size(); i<j; ++i)
    {
      float s = (float)_sizesFrom[i] + (float)(_sizesTo[i] - _sizesFrom[i]) * a;
      sizes << (int)s;
    }

  splitter->setSizes(sizes);
}



cceVisibilityHelper::cceVisibilityHelper (QWidget *target, bool visible, int time, int step)
  : cceSliderHelper (target, time, step)
  , _visible(visible)
{
}

void cceVisibilityHelper::update(QWidget *widget, float a)
{
  if (a >= 1.0f)
    {
      widget->setVisible(_visible);
    }
}

